﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CryForLua_wpf_AvEd.CodeCompletion
{
    /// <summary>
    /// Interaktionslogik für Window1.xaml
    /// </summary>
    public partial class ScriptBindLibWin : Window
    {
        public ScriptBindLibWin()
        {
            InitializeComponent();
            ReloadTreeView();


        }

        private void ReloadTreeView()
        {
            foreach (string functionTableKey in CodeTable.TABLE.Keys)
            {
                TreeViewItem functionTableItem = new TreeViewItem() { Header = functionTableKey };

                CodeTable.LuaFunctionTableBlah currentTable = new CodeTable.LuaFunctionTableBlah();

                CodeTable.TABLE.TryGetValue(functionTableKey, out currentTable);

                if (currentTable != null)
                {
                    foreach (CodeTable.LuaFunction function in currentTable.classFunctions)
                    {
                        TreeViewItem functionItem = new TreeViewItem()
                        {
                            Header = function.name,
                            ToolTip = function.description + "\n Returns:" + function.returnValue,
                        };

                        /*foreach (CodeTable.LuaParameter param in function.paramList)
                        {
                            TreeViewItem paramItem = new TreeViewItem() 
                            { 
                                Header = param.name,
                                ToolTip = param.description
                            };
                            functionItem.Items.Add(paramItem);
                        }*/

                        functionTableItem.Items.Add(functionItem);
                    }
                }

                contentTree.Items.Add(functionTableItem);
            }
        }

        private void OnSelectetItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            UpdateDataGrid();
        }

        private void UpdateDataGrid()
        {
            TreeViewItem currentItem = contentTree.SelectedItem as TreeViewItem;

            paramListBox.Items.Clear();

            TreeViewItem classItem = currentItem;
            while (classItem.Parent as TreeViewItem != null)
            {
                classItem = classItem.Parent as TreeViewItem;
            }

            if (classItem != null)
                this.Status.Text = classItem.Header.ToString();

            CodeTable.LuaFunction function = CodeTable.GetFunctionByClassAndName(classItem.Header.ToString(), currentItem.Header.ToString());

            if (function != null)
            {
                functionNameTextBox.Text = function.name;
                functionDescTextBox.Text = function.description;

                foreach (CodeTable.LuaParameter param in function.paramList)
                {
                    ListBoxItem item = new ListBoxItem()
                    { 
                        Content = param.name,
                        ToolTip = param.description,
                    };
                    paramListBox.Items.Add(item);
                }
            }
        }
    }
}
